package com.example.graphictest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;

public class MainActivity extends Activity {

	private MyView mMyView;
	private boolean isRunning = true;
    float deltaTime = 0;
    float startTime = 0;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mMyView = new MyView(this);

		setContentView(mMyView);
		
        new Thread(new Runnable() {
            public void run() {
                while(true){
                    float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f;
                    startTime = System.nanoTime();
                    
                    try {
                        Thread.sleep(10);
                        mMyView.update(deltaTime);
                        mMyView.postInvalidate();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
          }).start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onResume() {
		super.onResume();
		
//		while(isRunning)
//		{
//			mMyView.invalidate();
//		}
		

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		int action = event.getAction();

		// input position value translate
		// y = y-100 for tool bar height
		float x = event.getX();
		float y = event.getY() - 100;

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			// Do your stuff here
			mMyView.touchTriggerDown(x, y);
			break;
		case MotionEvent.ACTION_MOVE:
			// Do your stuff here
			mMyView.touchTriggerMove(x, y);
			break;
		case MotionEvent.ACTION_UP:
			// Do your stuff here
			mMyView.touchTriggerUp(x, y);
			break;
		}

		// redraw
		//mMyView.invalidate();

		return true;
	}

}
